package com.astrofizzbizz.stoneedgeqb;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.astrofizzbizz.simpleXml.SimpleXmlException;
import com.astrofizzbizz.simpleXml.SimpleXmlReader;
import com.astrofizzbizz.simpleXml.SimpleXmlWriter;

public class AstroTarget 
{
	private String name = null;
	private String decString = null;
	private String raString = null;
	private double epoch = 2000.0;
	private String filter = null;
	private double exposure = -1.0;
	private int bin = 1;
	private boolean dark = false;
	private int imageNo = -1;
	private double altLimit = 90.0;
	private String email;
	private Date reqDate = null;
	
	private double obsLatitude = 38.29341507;
	private double obsLongitude = -122.47058868;
	private boolean debug = false;
	private boolean verbose = false;

	public String getName() {return name;}
	public String getDecString() {return decString;}
	public String getRaString() {return raString;}
	public double getEpoch() {return epoch;}
	public String getFilter() {return filter;}
	public double getExposure() {return exposure;}
	public int getBin() {return bin;}
	public boolean isDark() {return dark;}
	public int getImageNo() {return imageNo;}
	public double getAltLimit() {return altLimit;}
	public String getEmail() {return email;}
	public Date getReqDate() {return reqDate;}
	public boolean isVerbose() {return verbose;}
	public boolean isDebug() {return debug;}
	
	public void setDebug(boolean debug) {this.debug = debug;}
	public void setVerbose(boolean verbose) {this.verbose = verbose;}
	
	public AstroTarget(SimpleXmlReader tag, boolean debug, boolean verbose) throws StoneEdgeException
	{
		this.debug = debug;
		this.verbose = verbose;
		try 
		{
			name = tag.attribute("name");
			decString = tag.attribute("dec");
			raString = tag.attribute("ra");
			epoch = Double.parseDouble(tag.attribute("epoch"));
			filter = tag.attribute("filter");
			exposure = Double.parseDouble(tag.attribute("exposure"));
			bin = Integer.parseInt(tag.attribute("bin"));
			dark = Boolean.parseBoolean(tag.attribute("dark"));
			imageNo = Integer.parseInt(tag.attribute("imageNo"));
			altLimit = Double.parseDouble(tag.attribute("altLimit"));
			email = tag.attribute("email");
			reqDate = readEuroDate(tag.attribute("reqDate"));
		} 
		catch (SimpleXmlException e) 
		{
			throw new StoneEdgeException(e);
		}
	}
	public void writeTag(SimpleXmlWriter xw)
	{
		xw.openXmlTag("target");
		xw.setAttribute("name", name);
		xw.setAttribute("decString", decString);
		xw.setAttribute("raString", raString);
		xw.setAttribute("epoch", Double.toString(epoch));
		xw.setAttribute("filter", filter);
		xw.setAttribute("exposure", Double.toString(exposure));
		xw.setAttribute("bin", Integer.toString(bin));
		xw.setAttribute("dark", Boolean.toString(dark));
		xw.setAttribute("imageNo", Integer.toString(imageNo));
		xw.setAttribute("altLimit", Double.toString(altLimit));
		xw.setAttribute("email", email);
		xw.setAttribute("reqDate", euroDateToString(reqDate));
		xw.closeXmlTag("target");
	}
	private Date readEuroDate(String euroDateString) throws StoneEdgeException 
	{
		DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try { date = df1.parse(euroDateString);} catch (ParseException e) {throw new StoneEdgeException();}
		return date;
	}
	private String euroDateToString(Date date)
	{
		DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df1.format(date);
	}

	public boolean targetDateExpired()
	{
		return false;
	}
	public boolean isVisible() throws StoneEdgeException 
	{
//TODO should check date with respect to observatory
		Date obsDate = new Date();
		boolean visible = true;
		double[] altAz = StarCoordUtilities.getAltAzDeg(StarCoordUtilities.raDeg(raString), StarCoordUtilities.decDeg(decString), 
				obsLongitude, obsLatitude, obsDate);
		
		if (altAz[0] < altLimit) 
		{
			visible = false;
		}
		else
		{
			String checkVisibleCommand = "echo \"" + raString + " " + decString + " " 
					+ Double.toString(epoch) + " " + name + "\" | visible";
			boolean linux = true;
			boolean getInfo = true;

			String[] checkVisibleOkDebugResponse = {"01:03:30 00:00:00 2000.000000 testTarg"};
			checkVisibleOkDebugResponse[0] = raString + " " + decString + " " + Double.toString(epoch) + " " + name;
		
			String[] info = Utilities.runExternalProcess(checkVisibleCommand, linux, getInfo, verbose, debug, checkVisibleOkDebugResponse);
			if (info == null) throw new StoneEdgeException("No Info from check visible command");
			int ilimitIndex = info[0].indexOf("limit");
			if (ilimitIndex >= 0) visible = false;
		}
		if (verbose && visible) System.out.println("Target " + name + " at alt = " + altAz[0] + " az = " + altAz[1]);

		return visible;
	}

}
